package com.example.administrator.testxingpiao.ui.perfom;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.SearchView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseActivity;
import com.example.administrator.testxingpiao.bean.GoodsListBean;
import com.example.administrator.testxingpiao.bean.PerformBean;
import com.example.administrator.testxingpiao.config.ConfigUrl;
import com.example.administrator.testxingpiao.ui.detail.PerformDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ContentActivity extends BaseActivity implements ContentContract.View {

    @BindView(R.id.Imag_back)
    ImageView Imag_back;
    @BindView(R.id.search)
    SearchView search;
    @BindView(R.id.menu)
    ImageView menu;
    @BindView(R.id.tv_sork)
    TextView tvSork;
    @BindView(R.id.sanjiao)
    ImageView sanjiao;
    @BindView(R.id.tv_selfirst)
    TextView tvSelfirst;
    @BindView(R.id.composing)
    ImageView composing;
    @BindView(R.id.mRv_perform)
    RecyclerView mRv_perform;
    private Context mContext = this;
    private Intent intent;
    private int position;
    private ContentModle modle;
    private ContentPresenter presenter;
    private PopupMenu popupMenu;
    private HashMap<String, String> params;
    private List<Map<String, String>> list;
    public static int ZONGHE = 0;
    public static int HTOL = 1;
    public static int LTOH = 2;
    private int count = 0;
    private List<GoodsListBean> goodsList;
    private List<GoodsListBean> goodListAll;
    private PerformAdapter adapter;
    private int countnum = 1;
    private Handler handler = new Handler();
    private GridLayoutManager manger;
    private int curpage = 1;
    private int PAGESIZE = 10;
    private List<PerformBean> performBeenlist=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        ButterKnife.bind(this);
        getSupportActionBar().hide();

        intent = getIntent();
        position = intent.getIntExtra("position", 0);
        position = intent.getIntExtra("position", 1);
        position = intent.getIntExtra("position", 2);
        goodsList = new ArrayList<>();
        goodListAll = new ArrayList<>();
        initData(0, "", "");
    }

    private void initData(int type, String key, String order) {
        modle = new ContentModle();
        presenter = new ContentPresenter(this, modle);

        if (type == 0) {
            presenter.initData(initParams(position, 0, "", ""));
        } else if (type == 1) {
            presenter.initData(initParams(position, 1, key, order));
        }
        log("进入initData方法");


    }


    private HashMap<String, String> initParams(int position, int type, String key, String order) {

        params = new HashMap<>();

        switch (position) {

            case 0:
                params.put(ConfigUrl.Key.ACT, "goods");
                params.put(ConfigUrl.Key.OP, "goods_list");
                params.put(ConfigUrl.Key.GC_ID, "15");
                params.put(ConfigUrl.Key.FROM, "app");
                params.put(ConfigUrl.Key.PAGE, "10");
                params.put(ConfigUrl.Key.CURPAGE, String.valueOf(curpage));
                params.put(ConfigUrl.Key.GC_ID, "15");
                if (type == 1) {
                    params.put(ConfigUrl.Key.KEY, key);
                    params.put(ConfigUrl.Key.ORDER, order);
                }
                break;
            case 1:
                params.put(ConfigUrl.Key.ACT, "goods");
                params.put(ConfigUrl.Key.OP, "goods_list");
                params.put(ConfigUrl.Key.GC_ID, "19");
                params.put(ConfigUrl.Key.FROM, "app");
                params.put(ConfigUrl.Key.PAGE, "10");
                params.put(ConfigUrl.Key.CURPAGE, String.valueOf(curpage));
                params.put(ConfigUrl.Key.GC_ID, "19");
                if (type == 1) {
                    params.put(ConfigUrl.Key.KEY, key);
                    params.put(ConfigUrl.Key.ORDER, order);
                }

                break;
            case 2:
                params.put(ConfigUrl.Key.ACT, "goods");
                params.put(ConfigUrl.Key.OP, "goods_list");
                params.put(ConfigUrl.Key.GC_ID, "9");
                params.put(ConfigUrl.Key.FROM, "app");
                params.put(ConfigUrl.Key.PAGE, "10");
                params.put(ConfigUrl.Key.CURPAGE, String.valueOf(curpage));
                params.put(ConfigUrl.Key.GC_ID, "9");
                if (type == 1) {
                    params.put(ConfigUrl.Key.KEY, key);
                    params.put(ConfigUrl.Key.ORDER, order);
                }

                break;

        }

        return params;
    }

    public void log(String msg) {
        Log.i("TAG", msg);
    }

    @OnClick({R.id.Imag_back, R.id.tv_sork, R.id.tv_selfirst, R.id.composing})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.Imag_back:
                finish();
                break;
            case R.id.tv_sork:
                popupMenu = new PopupMenu(getApplicationContext(), view);
                MenuInflater menuInflater = popupMenu.getMenuInflater();
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.zonghepaixu:
                                initData(0, "", "");

                                break;
                            case R.id.hight_to_low:
                                initData(1, "3", "2");
                                //Log.i("tag","1111"+initParams(3,1,"1","2"));
                                break;
                            case R.id.low_to_hight:
                                initData(1, "3", "1");
                                break;
                            case R.id.person_moods:
                                initData(1, "2", "2");
                                break;
                        }
                        return false;
                    }
                });
                menuInflater.inflate(R.menu.popupmenu_content_tvsort, popupMenu.getMenu());
                popupMenu.show();
            case R.id.tv_selfirst:
                initData(1, "1", "2");
                changeItem(performBeenlist.get(position));
                break;
            case R.id.composing:
                if (countnum == 1) {
                    countnum = 2;
                } else {
                    countnum = 1;
                }

                    changeItem(performBeenlist.get(position));


                break;

        }

    }


    @Override
    public void onSuccessGetContent(PerformBean performBean) {
        count++;
        performBeenlist.add(performBean);
        changeItem(performBean);
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                switch (position) {
                    case 0:
                        Log.i("tag","adaptredfsaf");
                        Intent intent0 = new Intent(mContext, PerformDetailActivity.class);
                        intent0.putExtra("id", ContentActivity.this.goodsList.get(i).getGoodsId());
                        mContext.startActivity(intent0);
                        break;
                    case 1:
                        Intent intent1 = new Intent(mContext, PerformDetailActivity.class);
                        intent1.putExtra("id", ContentActivity.this.goodsList.get(i).getGoodsId());
                        mContext.startActivity(intent1);
                        break;
                    case 2:
                        Intent intent2 = new Intent(mContext, PerformDetailActivity.class);
                        intent2.putExtra("id", ContentActivity.this.goodsList.get(i).getGoodsId());
                        mContext.startActivity(intent2);
                        break;
                }

            }
        });
    }

    private void changeItem(final PerformBean performBean) {
        if (count > 1) {
            goodsList.clear();
        }
        GridLayoutManager manager = new GridLayoutManager(mContext, countnum);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRv_perform.setLayoutManager(manager);
        goodsList.addAll(performBean.getDatas().getGoodsList());
        adapter = new PerformAdapter(R.layout.list_item_perform,goodsList);
        adapter.openLoadMore(1  , true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                curpage++;
                initData(0, "", "");

                mRv_perform.post(new Runnable() {
                    @Override
                    public void run() {

                        if (10<30) {
                            goodsList.addAll(performBean.getDatas().getGoodsList());
                            adapter.notifyDataChangedAfterLoadMore(goodsList,true);

                       }else {
                           goodListAll.addAll(performBean.getDatas().getGoodsList());
                           adapter.notifyDataSetChanged();
                       }


                       Log.i("tag",curpage+"------------");

                    }

                });
            }
        });
        mRv_perform.setAdapter(adapter);
       // adapter.notifyDataSetChanged();
    }


    @Override
    public void onFailGetPerform(String msg) {

    }

    @Override
    protected void onDestroy() {
        count = 0;
        super.onDestroy();
    }
}